package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum ReceiverUpdateCode implements EnumTrans<CompleteEntity.ReceiverUpdateCode, ReceiverUpdateCode> {
    /**
     * 未知错误
     * 未进行升级
     * 升级成功(包含升级成功、升级完成、写入完成、加载完成（无需重启）)
     * 未探测到接收卡
     * 接收卡不支持升级
     * 擦除失败（不要断电，需重新升级）
     * 写入失败（不要断电，需重新升级）
     * 校验失败（不要断电，需重新升级）
     * 加载失败（需手动重启）
     * 接收卡版本不一致（不支持一起升级）
     * 获取接收卡信息失败
     * 文件不合法
     * 文件已损坏
     */
    FAIL_REASON_0(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_0),
    FAIL_REASON_1(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_1),
    UPDATE_SUCCESS(CompleteEntity.ReceiverUpdateCode.UPDATE_SUCCESS),
    FAIL_REASON_5(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_5),
    FAIL_REASON_6(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_6),
    FAIL_REASON_7(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_7),
    FAIL_REASON_8(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_8),
    FAIL_REASON_9(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_9),
    FAIL_REASON_10(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_10),
    FAIL_REASON_12(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_12),
    FAIL_REASON_13(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_13),
    FAIL_REASON_14(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_14),
    FAIL_REASON_15(CompleteEntity.ReceiverUpdateCode.FAIL_REASON_15),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.ReceiverUpdateCode receiverUpdateCode;

    ReceiverUpdateCode(CompleteEntity.ReceiverUpdateCode receiverUpdateCode) {
        this.id = receiverUpdateCode.getNumber();
        this.name = receiverUpdateCode.name();
        this.receiverUpdateCode = receiverUpdateCode;
    }

    /**
     * @param receiverUpdateCode
     * @return 对应的类型
     */
    public static ReceiverUpdateCode getReceiverUpdateCode(CompleteEntity.ReceiverUpdateCode receiverUpdateCode) {
        Objects.requireNonNull(receiverUpdateCode, "receiverUpdateCode can not be null.");
        return Arrays.stream(ReceiverUpdateCode.values())
                .filter(e -> e.name.equals(receiverUpdateCode.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.ReceiverUpdateCode toProto() {
        return this.receiverUpdateCode;
    }

    @Override
    public ReceiverUpdateCode formProto(CompleteEntity.ReceiverUpdateCode receiverUpdateCode) {
        return getReceiverUpdateCode(receiverUpdateCode);
    }
}


